package com.origin.app.business.net

import android.util.Log
import com.jeremyliao.liveeventbus.LiveEventBus
import com.origin.app.R
import com.origin.app.databinding.ActivityDownLoadBinding
import com.origin.baselibrary.activity.style.ViewModelActivity
import com.origin.netlibrary.NetUtils
import com.origin.netlibrary.bean.DownLoadProBean
import com.origin.netlibrary.download.DownSaveFileType
import com.origin.netlibrary.download.DownloadManager
import com.origin.utils.log.LogUtils
import com.origin.utils.statusBar.StatusBarUtil
import java.io.File

class DownLoadActivity :
    ViewModelActivity<ActivityDownLoadBinding, NetViewModel>(R.layout.activity_down_load) {
    val DOWNLOAD_URL =
        "http://file.x8box.com/x8/tool/tools/cb6971aca7d03f352f599d87d1d1e90a.zip"
    val DOWNLOAD_URL_QQ =
        "https://639b204c662da64272142a5f83848b5d.rdt.tfogc.com:49156/downv6.qq.com/qqweb/QQ_1/android_apk/Android_8.9.53.10815_537158635_64.apk"
    var type = 0
    override fun initContentView() {
        StatusBarUtil.setLightMode(this)
        contentViewBinding.downBtn.setOnClickListener {

            val filePath = File(
                NetUtils.getApplication()
                    .getExternalFilesDir(DownSaveFileType.getTypeFileStr(DownSaveFileType.OTHER_TYPE))!!
                    .absolutePath
            )
            if (!filePath.exists()) {
                val isMkdirs = filePath.mkdirs()
                Log.e("asdas", filePath.absolutePath + "    文件创建  " + isMkdirs)
            }

            val fileName: String = DOWNLOAD_URL.substring(DOWNLOAD_URL.lastIndexOf('/') + 1)
            val outFile = File(filePath.absolutePath + "/" + fileName)
            LogUtils.__e("保存文件路径：${outFile.absolutePath}")


            if(DownloadManager.isDownLoading(DOWNLOAD_URL,outFile.absolutePath)){
                DownloadManager.pause(DOWNLOAD_URL,outFile.absolutePath)
            }else{
                DownloadManager.startDownLoad(DOWNLOAD_URL,outFile.absolutePath)
            }


        }
        contentViewBinding.prTv.text = "1"
        LiveEventBus.get(DownLoadProBean::class.java).observe(this){
            if (it.isDone){
                contentViewBinding.prTv.text = "下载完成"
            } else if(it.isPause){
                contentViewBinding.prTv.text = "暂停"
            } else{
                contentViewBinding.prTv.text = "${it.progress}%"
            }

        }
    }

    override fun pageLogical() {
        super.pageLogical()

    }

    override fun onDestroy() {
        super.onDestroy()

    }

}